home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / bsynz.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  3KB  |  147 lines

  1. /******************************************************************
  2. *
  3. *    BSYNZ Version 49
  4. *
  5. ******************************************************************
  6. *
  7. *   Synthesize One Pitch Epoch
  8. *
  9. * Inputs:
  10. *  COEF  - Predictor coefficients
  11. *  IP    - Pitch period (number of samples to synthesize)
  12. *  IV    - Voicing for the current epoch
  13. *  RMS   - Energy for the current epoch
  14. *  ORDER - Synthesizer filter order (number of PC's)
  15. *  RATIO - Energy slope for plosives
  16. *  G2PASS- Sharpening factor for 2 pass synthesis
  17. * Outputs:
  18. *  SOUT  - Synthesized speech
  19. */
  20.  
  21. #include "config.ch"
  22. /*#include "common.h"*/
  23. #include "lpcdefs.h"
  24. #include <math.h>
  25.  
  26. /*
  27. #define MESCL 1.0
  28. #define PESCL 1.0
  29. */
  30. float kexc[25]={
  31. 8,-16,26,-48,86,-162,294,-502,718,-728,
  32.               184,672,-610,-672,184,728,718,502,294,162,
  33.               86,48,26,16,8
  34. };
  35.  
  36. extern float exc[MAXPIT+MAXORD], exc2[MAXPIT+MAXORD];
  37.  
  38. bsynz( coef, ip, iv, sout, rms, ratio, g2pass )
  39. int ip, iv;
  40. float coef[], sout[], g2pass, rms, ratio;
  41. {
  42. int px;
  43. static int ipo=0;
  44. int i, j, k;
  45. float noise[MAXPIT+MAXORD];
  46. float lpi0, hpi0;
  47. float a0=.125, a1=.75, a2=.125/*, a3=0*/, b0=-.125, b1=.25, b2=-.125/*, b3=0*/;
  48. float pulse, sscale, xssq, sum, ssq, gain;
  49. float xy;
  50. static float rmso=0.0, lpi1=0.0, lpi2=0.0, /*lpi3,*/ hpi1=0.0, hpi2=0.0/*, hpi3*/;
  51.  
  52.  
  53. /*  Calculate history scale factor XY and scale filter state    */
  54.  
  55. xy = mmin((rmso/(rms+.000001)), 8.0);
  56. rmso = rms;
  57. for(i=0;i<ORDER;i++)
  58.        exc2[i] = exc2[ipo+i]*xy;
  59.  
  60. ipo = ip;
  61.  
  62. if(iv==0) {
  63.  
  64. /*  Generate white noise for unvoiced    */
  65.  
  66.     for(i=0;i<ip;i++)
  67.         /*exc[ORDER+i] = (int)(Rrandom() * 0.015625);*/
  68.         exc[ORDER+i] = Rrandom() >>6;
  69.     
  70. /*  Impulse doublet excitation for plosives    */
  71.  
  72.     px = ((Rrandom()+32768)*(ip-1)>>16) + ORDER + 1;
  73.     /*pulse = PESCL*(ratio*.25)*342;*/
  74.     pulse = ratio*85.5;
  75.     if(pulse>2000) pulse = 2000;
  76.     exc[px-1]   += pulse;
  77.     exc[px] -= pulse;
  78.  
  79. /*  Load voiced excitation    */
  80. }
  81. else    {
  82.     /*sscale = sqrt((float)ip)/6.928;*/
  83.     sscale = sqrt((float)ip)*0.144341801;
  84.     for(i=0;i<ip;i++)    {
  85.         exc[ORDER+i] = 0.;
  86.         if(i<=25) exc[ORDER+i] = sscale*kexc[i];
  87.         lpi0 = exc[ORDER+i];
  88.         exc[ORDER+i] = a0*exc[ORDER+i] + a1*lpi1 + a2*lpi2 /*+ a3*lpi3*/;
  89.         /*lpi3 = lpi2;*/
  90.         lpi2 = lpi1;
  91.         lpi1 = lpi0;
  92.     }
  93.     for(i=0;i<ip;i++)    {
  94.         /*noise[ORDER+i] = MESCL * (int)(Rrandom() * 0.015625);*/
  95.         noise[ORDER+i] = Rrandom() >>6;
  96.         hpi0 = noise[ORDER+i];
  97.         noise[ORDER+i] = b0*noise[ORDER+i] + b1*hpi1 + b2*hpi2 /*+ b3*hpi3*/;
  98.         /*hpi3 = hpi2; */
  99.         hpi2 = hpi1;
  100.         hpi1 = hpi0;
  101.     }
  102.     for(i=0;i<ip;i++)
  103.         exc[ORDER+i] += noise[ORDER+i];
  104.     
  105. }
  106.  
  107. /*   Synthesis filters:
  108. *    Modify the excitation with all-zero filter  1 + G*SUM    */
  109.  
  110. xssq = 0;
  111. for(i=0;i<ip;i++)    {
  112.     k = ORDER + i;
  113.     sum = 0.;
  114.     for(j=0;j<ORDER;j++)
  115.         sum += coef[j]*exc[k-j-1];
  116.     sum *= g2pass;
  117.     exc2[k] = sum + exc[k];
  118. }
  119.  
  120. /*   Synthesize using the all pole filter  1 / (1 - SUM)    */
  121.  
  122. for(i=0;i<ip;i++)    {
  123.     k = ORDER + i;
  124.     sum = 0.;
  125.     for(j=0;j<ORDER;j++)
  126.         sum += coef[j]*exc2[k-j-1];
  127.     exc2[k] += sum;
  128.     xssq = xssq + exc2[k]*exc2[k];
  129. }
  130.  
  131. /*  Save filter history for next epoch    */
  132.  
  133. for(i=0;i<ORDER;i++)    {
  134.         exc[i] = exc[ip+i];
  135.         exc2[i] = exc2[ip+i];
  136. }
  137.  
  138. /*  Apply gain to match RMS    */
  139.  
  140. ssq = rms*rms*ip;
  141. gain = sqrt(ssq/xssq);
  142. for(i=0;i<ip;i++)
  143.         sout[i] = gain*exc2[ORDER+i];
  144.  
  145.  
  146. }
  147.